package com.example.moblie2030606150cjk0618;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class UsersRViewAdapter extends RecyclerView.Adapter<UsersRViewAdapter.ViewHolder> {
    private List<Users> list;
    public UsersRViewAdapter(List<Users> list) {
        this.list = list;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvItemnId, tvItemNane , tvItemAge, tvItemScore ;
        Button btnItemUpdate, btnItemDelete;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvItemnId = itemView.findViewById(R.id.tvItemId);
            tvItemNane = itemView.findViewById(R.id.tvItemName);
            tvItemAge = itemView.findViewById(R.id.tvItemAge);
            tvItemScore = itemView.findViewById(R.id.tvItemScore);
            btnItemDelete = itemView.findViewById(R.id.btnItemDelete);
            btnItemUpdate = itemView.findViewById(R.id.btnItemUpdate);
        }
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_main_user, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull UsersRViewAdapter.ViewHolder holder, int position) {
        Users users = list.get(position);
        holder.tvItemnId.setText(String.valueOf(users.get_id()));
        holder.tvItemNane.setText(users.getName());
        holder.tvItemAge.setText(String.valueOf(users.getAge()));
        holder.tvItemScore.setText(String.valueOf(users.getScore()));
        holder.btnItemDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MyDataAdapter().delete(v.getContext(), list.get(position).get_id());
                list.clear();
                list.addAll(new MyDataAdapter().QueryAll(v.getContext()));
                notifyDataSetChanged();
            }
        });
        holder.btnItemUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final View view = LayoutInflater.from(v.getContext()).inflate(R.layout.dialog_insert, null);
                final EditText edTxtDialogName = view.findViewById(R.id.edtTxtDialogName);
                final EditText edTxtDialogAge = view.findViewById(R.id.edtTxtDialogAge);
                final EditText edTxtDialogScore = view.findViewById(R.id.edtTxtDialogScore);
                edTxtDialogName.setText(list.get(position).getName());
                edTxtDialogAge.setText(String.valueOf(list.get(position).getAge()));
                edTxtDialogScore.setText(String.valueOf(list.get(position).getScore()));
                new AlertDialog.Builder(v.getContext())
                        .setTitle("更新数据")
                        .setView(view)
                        .setPositiveButton("更新", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String name = edTxtDialogName.getText().toString().trim();
                                int age = Integer.valueOf(edTxtDialogAge.getText().toString().trim());
                                float score = Float.valueOf(edTxtDialogScore.getText().toString().trim());
                                new MyDataAdapter().update(v.getContext(), list.get(position).get_id(), new Users(name, age, score));
                                list.clear();
                                list.addAll(new MyDataAdapter().QueryAll(v.getContext()));
                                notifyDataSetChanged();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
